package reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

/**
 * @Author:zimmor
 * @Date: 2021/3/25 15:05
 * @Description 展示类包含的所有内容
 * @version: 1.0.0
 */

public class ShowClassUtil {
    public static void showAll(Class clazz){
        showFields(clazz);
        System.out.println("==============================");
        showConstructor(clazz);
        System.out.println("==============================");
        showMethods(clazz);
    }
    public static void showFields(Class clazz){
        System.out.println(clazz.getName()+"类的所有字段");
        for (Field field : clazz.getDeclaredFields()) {
            System.out.print(field.getType().toString()+"  ");
            System.out.println(field.getName());
        }
    }
    public static void showMethods(Class clazz){
        System.out.println(clazz.getName()+"类的公共方法如下：");
        StringBuffer sb = new StringBuffer();
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            sb.append(method.getName()+"(");
            if(method.getParameterCount()>0) {
                for (Parameter parameter : method.getParameters()) {
                    sb.append(parameter.toString());
                    sb.append(", ");
                }
                sb.delete(sb.length()-2,sb.length());
            }
            sb.append(")");
            System.out.println(sb.toString());
           sb.setLength(0);
        }
    }
    public static void showConstructor(Class clazz) {

        System.out.println(clazz.getName()+"类的公共构造方法如下：");
        Constructor<?>[] constructors = clazz.getConstructors();
        for (Constructor<?> constructor : constructors) {
            String constructorMethod = "";
            constructorMethod = constructor.getName()+"(";
            if(constructor.getParameterCount()>0){
                Parameter[] parameters = constructor.getParameters();
                for (Parameter parameter : parameters) {
                    constructorMethod += parameter.toString()+", ";
                }
                constructorMethod = constructorMethod.substring(0,constructorMethod.length()-2);
            }
            constructorMethod += ")";
            System.out.println(constructorMethod);
        }
    }
}
